home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / gui / pmdev.lha / Demos / Dynamic.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-21  |  3.6 KB  |  137 lines

  1. /*
  2.  *
  3.  * $VER: Dynamic.c 1.1 (20.2.98)
  4.  *
  5.  * Popup Menu example program
  6.  *
  7.  * ©1996-1998 Henrik Isaksson
  8.  * All Rights Reserved.
  9.  *
  10.  * Run and click the mouse in the window!
  11.  *
  12.  */
  13.  
  14. #include <intuition/intuition.h>
  15. #include <exec/memory.h>
  16.  
  17. #include <clib/intuition_protos.h>
  18. #include <clib/exec_protos.h>
  19. #include <clib/alib_protos.h>
  20.  
  21. #include <string.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24.  
  25. #include <libraries/pm.h>
  26. #include <proto/pm.h>
  27.  
  28. struct IntuitionBase    *IntuitionBase;
  29. struct GfxBase        *GfxBase;
  30. struct PopupMenuBase    *PopupMenuBase;
  31.  
  32. struct Window *w;    // This window is only needed to find out when and where the menu should appear.
  33.             // The font in this window's rastport will be used for the menu.
  34.  
  35. struct PopupMenu * __saveds __asm SubConstructFunc(register __a0 struct Hook *hook,
  36.                      register __a2 struct PopupMenu *selected)
  37. {
  38.     struct PopupMenu *newpm;
  39.     char bfr[128];
  40.  
  41.     // Don't try to open windows, requesters or printing text from this
  42.     // hook, wich is called while the menu is still open. (ofcourse)
  43.     // The display might be locked!
  44.  
  45.     // A random number to prove that it works...
  46.     sprintf(bfr, "Random number: %ld", rand());
  47.  
  48.     newpm=PM_MakeMenu(
  49.         PMItem("It works"),    PMEnd,
  50.         PMItem("Yes, it does!"), PMEnd,
  51.         PMItem(bfr),        PMEnd,
  52.     PMEnd;        
  53.  
  54.     if(newpm) {
  55.         if(selected->Sub) {
  56.             // We don't have to worry about freeing the new menu,
  57.             // as it will be freed automatically. But we do have
  58.             // to take care of the old menu, since the library
  59.             // won't know it exists.
  60.             PM_FreePopupMenu(selected->Sub);
  61.         }
  62.         selected->Sub=newpm;
  63.     }
  64.  
  65.     return selected->Sub;    // If you return NULL, no menu will be opened.
  66. }
  67.  
  68. struct Hook SubConstruct;
  69.  
  70. struct PopupMenu *MakeTestMenu(void);
  71.  
  72. void main()
  73. {
  74.     struct IntuiMessage *im,imsg;
  75.     struct PopupMenu *p;
  76.     BOOL r=TRUE;
  77.  
  78.     SubConstruct.h_Entry=(HOOKFUNC)SubConstructFunc;
  79.     SubConstruct.h_Data=NULL;    // User Data - you can put anything you like to here.
  80.  
  81.     PopupMenuBase=(struct PopupMenuBase *)OpenLibrary(POPUPMENU_NAME,POPUPMENU_VERSION);            // Open the library
  82.     if(PopupMenuBase) {
  83.         IntuitionBase=(struct IntuitionBase *)PopupMenuBase->pmb_IntuitionBase;    // We let popupmenu.library open the libraries we need
  84.         GfxBase=(struct GfxBase *)PopupMenuBase->pmb_GfxBase;            // They remain valid until the library is closed!
  85.  
  86.         p=MakeTestMenu(); // Declared at the end of this file.
  87.  
  88.         if(p) {
  89.             w=OpenWindowTags(NULL,    WA_IDCMP,    IDCMP_CLOSEWINDOW|IDCMP_MOUSEBUTTONS|IDCMP_VANILLAKEY,    // Open a little window
  90.                     WA_RMBTrap,    TRUE,
  91.                     WA_DragBar,    TRUE,
  92.                     WA_Width,    150,
  93.                     WA_Height,    100,
  94.                     WA_Left,    0,
  95.                     WA_Top,        100,
  96.                     WA_Title,    "Dynamic Menus",
  97.                     WA_CloseGadget,    TRUE,
  98.                     TAG_DONE);
  99.             if(w) {
  100.                 while(r) {
  101.                     WaitPort(w->UserPort);                        // Wait for a message
  102.                     while((im=(struct IntuiMessage *)GetMsg(w->UserPort))) {    // Get the message
  103.                         CopyMem(im,&imsg,sizeof(struct IntuiMessage));        // Copy the contents of it
  104.                         ReplyMsg((struct Message *)im);                // Reply the message
  105.  
  106.                         if(imsg.Class==IDCMP_MOUSEBUTTONS) {
  107.                             PM_OpenPopupMenu(w,
  108.                                 PM_Menu,        p,
  109.                                 TAG_DONE);
  110.                         }
  111.                         if(imsg.Class==IDCMP_CLOSEWINDOW) r=FALSE;        // See if the user wants to quit
  112.                     }
  113.                 }
  114.                 CloseWindow(w);
  115.             } else printf("Window error!\n");
  116.             PM_FreePopupMenu(p);
  117.         } else printf("Menu error!\n");
  118.         CloseLibrary((struct Library *)PopupMenuBase);
  119.     }
  120. }
  121.  
  122. struct PopupMenu *MakeTestMenu()
  123. {
  124.     struct PopupMenu *p;
  125.  
  126.     p=PM_MakeMenu(
  127.         PMItem("Dynamic submenu"),
  128.             PM_SubConstruct,    &SubConstruct,
  129.             PMSimpleSub,
  130.                 PMInfo("This won't work unless"),    End,    // this will be shown if the demo doesn't work
  131.                 PMInfo("you get version 7.4!"),        End,    // (maybe an old version of the library)
  132.             End,
  133.         End,
  134.     End;
  135.  
  136.     return p;
  137. }